/**
 * 总体路由处理器
 */
import { Route, RouteConfig } from 'vue-router'
import { setStore } from '@/utils/store'
import { ITagView } from '@/store/modules/tags-view'

export const resolveUrlPath = (url:string, name:string) => {
  let reqUrl = url
  if (url.indexOf('#') !== -1 && url.indexOf('http') === -1) {
    // const port = reqUrl.substr(reqUrl.indexOf(':'))
    // reqUrl = `/myiframe/urlPath?src=${baseUrl}${port}${reqUrl.replace('#', '').replace(port, '')}}&name=${name}`
  } else if (url.indexOf('http') !== -1) {
    reqUrl = `/myiframe/urlPath?src=${reqUrl}&name=${name}`
  } else {
    reqUrl = `${reqUrl}`
  }
  // return reqUrl
  return reqUrl
}

/**
 * 总体路由设置器
 */
export const setUrlPath = ($route: Route) => {
  let value = ''
  if ($route.query.src) {
    value = $route.query.src as string
  } else {
    value = $route.path.replace(/( )/ig, '%20')
  }
  return value
}

export const saveTags = (visitedViews: ITagView[]): Promise<void> => {
  return new Promise((resolve, reject) => {
    const tags = visitedViews.map(item => {
      return {
        title: item.title,
        path: item.path,
        query: item.query,
        fullPath: item.fullPath,
        meta: item.meta
      }
    })
    setStore({ name: 'main_tags', content: tags, type: 'session' })
    resolve()
  })
}
